package org.dragonnova.meetingserver.utils;

import android.app.ActivityManager;
import android.content.Context;

import java.util.List;

/**
 * Created by hucn on 2016/3/16.
 * Description:
 */
public class PackageUtil {

    /**
     * 这个方法无法返回正确的状态
     * 1:程序在前台运行
     * 2:程序在后台运行
     * 3:程序未启动
     */
    public static int getAppSatus(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(context.getPackageName())) {
                if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
                    LogUtil.eee("background：" + appProcess.processName);
                    return 1;
                }else{
                    LogUtil.eee("foreground：" + appProcess.processName);
                    return 2;
                }
            }
        }
        LogUtil.eee("activity not started");
        return 3;
    }

    /**
     * 用来判断服务是否运行
     * 判断的服务名字
     * true：在运行
     * false：不在运行
     */
    public static boolean getServiceStatus(Context mContext, String className) {
        boolean isRunning = false;
        ActivityManager activityManager = (ActivityManager)
                mContext.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> serviceList
                = activityManager.getRunningServices(30);
        if (!(serviceList.size() > 0)) {
            return false;
        }
        for (int i = 0; i < serviceList.size(); i++) {
            if (serviceList.get(i).service.getClassName().equals(className)) {
                isRunning = true;
                break;
            }
        }
        return isRunning;
    }
}
